package com.ms.pda.utils

import android.content.Context
import android.view.View
import androidx.annotation.NonNull
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.StringUtils
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.enums.PopupPosition
import com.lxj.xpopup.interfaces.XPopupCallback
import com.ms.pda.components.*
import com.ms.pda.interf.OnDialogCallback
import com.ms.pda.interf.OnSelectCallback
import com.ms.pda.net.ResponseError

object DialogUtil {

    fun getErrorDialog(
        context: Context,
        error: ResponseError,
        from: Int?,
    ): BasePopupView {
        return XPopup.Builder(context)
            .isDestroyOnDismiss(true)
            .setPopupCallback(object : XPopupCallback {
                override fun onCreated(popupView: BasePopupView) {
                    val popup: WarnDialog = popupView as WarnDialog
                    if (error.errorCode == 2) {
                        popup.setContent(
                            StringUtils.format(
                                "%d:%s", error.errorCode,
                                "提交内容错误"
                            )
                        )
                    } else {
                        popup.setContent(
                            StringUtils.format(
                                "%d:%s",
                                error.errorCode,
                                error.errorMsg
                            )
                        )
                    }
                }

                override fun beforeShow(popupView: BasePopupView) {}
                override fun onShow(popupView: BasePopupView) {}
                override fun onDismiss(popupView: BasePopupView) {}
                override fun beforeDismiss(popupView: BasePopupView) {}
                override fun onBackPressed(popupView: BasePopupView): Boolean {
                    return false
                }

                override fun onKeyBoardStateChanged(popupView: BasePopupView, height: Int) {}
                override fun onDrag(
                    popupView: BasePopupView,
                    value: Int,
                    percent: Float,
                    upOrLeft: Boolean
                ) {
                }

                override fun onClickOutside(popupView: BasePopupView) {}
            })
            .asCustom(WarnDialog(context, error.errorCode, error.throwable, from))
            .show()
    }


    fun getTipPopupView(
        @NonNull context: Context,
        content: String?,
        callback: OnDialogCallback?
    ): BasePopupView? {
        return XPopup.Builder(context)
            .isDestroyOnDismiss(true)
            .dismissOnTouchOutside(false)
            .dismissOnBackPressed(false)
            .asCustom(ConfirmDialog(context, content, callback, true)).show()
    }

    fun getTipPopupView(
        @NonNull context: Context,
        content: String?,
        isHideCancel: Boolean,
        callback: OnDialogCallback?
    ): BasePopupView? {
        return XPopup.Builder(context)
            .isDestroyOnDismiss(true)
            .dismissOnTouchOutside(true)
            .dismissOnBackPressed(true)
            .asCustom(ConfirmDialog(context, content, callback, isHideCancel)).show()
    }

    fun getTipPopupView(
        @NonNull context: Context,
        content: String?,
        isHideCancel: Boolean,
        callback: OnDialogCallback?,
        confirmBtn: String?
    ): BasePopupView? {
        return XPopup.Builder(context)
            .isDestroyOnDismiss(true)
            .dismissOnTouchOutside(true)
            .dismissOnBackPressed(true)
            .asCustom(ConfirmDialog(context, content, callback, isHideCancel, confirmBtn)).show()
    }

    fun getSelectPopupView(
        @NonNull context: Context,
        title: String,
        left: String,
        right: String,
        callback: OnSelectCallback?
    ): BasePopupView? {
        return XPopup.Builder(context)
            .isDestroyOnDismiss(true)
            .dismissOnTouchOutside(true)
            .dismissOnBackPressed(true)
            .asCustom(SelectModeDialog(context, title, left, right, callback)).show()
    }

    fun getUpdatePopupView(
        @NonNull context: Context
    ): BasePopupView? {
        return XPopup.Builder(context)
            .isDestroyOnDismiss(true)
            .dismissOnTouchOutside(false)
            .dismissOnBackPressed(false)
            .asCustom(UpdaterDialog(context)).show()
    }
}